<?php

/**
 * 
 * @author yuxun
 *
 */
class SettingSpecialController extends AdminBaseController {
	
	public function index(){
		$this->setTitle('专服配置(热更)');
		
		$filter = new PubFilter();
		$grid   = new PubGrid();
		
		$filter->addFilter('name', 'input', '专服名称');
		
		$params = $filter->get();
		$page = $this->R('page', '1');
		$m = $this->R('m');
		$params['m'] = $m;
		$header = SettingSpecialService::indexHeader();
		if(PubFunc::isAjax()){
			$result = SettingSpecialService::indexData($params);
			$this->out = array(
					'count'     => $result['count'],
					'data'      => $result['data'],
					'result'    => true
			);
			$this->outType = 'json';
		}else{
			$order  = 'create_time';
			$url = PubFunc::ajaxUrl("?r=settingSpecial/index", $params);
			$grid->addGrid($header, $url, array(
					'name'=>'数据',
					'order'=>array($order, 'desc'),
					'type'=>'settingSpecial/index',
					'page'=>$page
			));
			 
			$this->out['filter'] = $filter->show();
            $this->out['grid'] = $grid->show();

		}
	}

    public function add(){
        $this->setTitle('新建专服配置');

    }

    public function doAdd(){  	
    	$count = SettingSpecialService::repeat(0, 'special_id', $this->R('special_id'),$this->R('g'));
    	if($count > 0){
    		$this->showMessage('专服ID已经存在');
    	}else{
    		$result = SettingSpecialService::doAdd($_POST);
    		
    		if($result !== false){
    			$this->showMessage('新增成功', PubFunc::buildUrl("/oss/?r=settingSpecial/index"));
    		}else{
    			$this->showMessage('新增失败');
    		}
    	}
        
    }

    public function edit(){
        $id = $this->R('id');
        $info = SettingSpecialService::get($id);

        if(empty($info)) $this->showMessage('找不到该信息');
        
        $modSettingChannel = new ModSettingChannel();
        $list_obj = $modSettingChannel->findAllByAttributes(array('special_id'=>$id));
        if($list_obj){
        	$this->out['is_edit'] = 2;  //不能修改
        }else{
        	$this->out['is_edit'] = 1;
        }

        $this->setTitle('编辑专服', $info['name']);

        $this->out['info'] = $info;
        
        $backUrl = PubFunc::buildBackUrl("/oss/?r=settingSpecial/index");  //返回路径
        $this->out['backUrl'] = $backUrl;
        
        $page = $this->R('page');
        $this->out['page'] = $page;
        
    }

    public function doEdit(){
        $id = $this->R('id');
        $count = SettingSpecialService::repeat($this->R('id'), 'special_id', $this->R('special_id'),$this->R('g'));
        if($count > 0){
        	$this->showMessage('专服ID已经存在');
        }else{
        	$result = SettingSpecialService::doEdit($id, $_POST);
        	
        	if($result['state'] !== false){
        		$this->showMessage($result['msg'], PubFunc::buildBackUrl("/oss/?r=settingSpecial/index"));
        	}else{
        		$this->showMessage($result['msg']);
        	}
        } 
    }
    
    public function delete(){
        $id = $this->R('id');
        
        $modSettingChannel = new ModSettingChannel();
        $list_obj = $modSettingChannel->findAllByAttributes(array('special_id'=>$id));
        if($list_obj){
        	$this->showMessage('专服已经被使用，不能进行删除', PubFunc::buildBackUrl("/oss/?r=settingSpecial/index"));
        }else{
        	$result = SettingSpecialService::delete($id);
        	if($result !== false){
        		$this->showMessage('删除成功', PubFunc::buildBackUrl("/oss/?r=settingSpecial/index"));
        	}else{
        		$this->showMessage('删除失败', PubFunc::buildBackUrl("/oss/?r=settingSpecial/index"));
        	}
        }
    }
    
    /**
     * 同步操作
     */
    public function synSpecial(){
    	$ids = $this->R('ids');
    	$out = SettingSpecialService::synSpecial($ids);
    	$this->out = $out;
    	$this->outType = 'json';
    }

}